package com.example.gclove.controller;

import com.example.gclove.mapper.ThingMapper;
import com.example.gclove.pojo.Thing;
import com.example.gclove.service.ThingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/apit")
@CrossOrigin("*")
public class ThingController {

    //必须要在mapper下加MAPPER的注释才可以使用
    @Autowired
    private ThingMapper thingMapper;

    @Autowired
    private ThingService thingService;


    //按照id号查找该用户的所有所有事件
    @PostMapping("/allThing")
    public Map selectAllThing(@RequestParam int myUserId, @RequestParam int taUserId) {
        Map map = new HashMap<>();
        List list;
        list = thingMapper.selectByThingUserId(myUserId);
        if (list.size() == 0) {
            list.clear();
        }
        Integer a = thingMapper.selectSumByUserId(myUserId);
        Integer b = thingMapper.selectSumByUserId(taUserId);
        map.put("aList", list);
        if (a == null) {
            a = 0;
        }
        if (b == null) {
            b = 0;
        }
        map.put("myTotal", a);
        map.put("taTotal", b);
        return map;
    }

    //这里对应详细页面中的提交功能,会修改数据库的数据
    @PostMapping("/changeThing")
    public void changeThing(@RequestBody Thing thing) {
        thingMapper.updateThing(thing);
    }


    //对应添加事件的功能
    @PostMapping("/addThings")
    public void addThings(@RequestBody Thing thing) {
        if (thing.getThingScore() > 0) {
            thing.setThingType(1);
        } else {
            thing.setThingType(0);
        }
        thing.setThingNowTotal(thing.getThingNowTotal() + thing.getThingScore());
        thingMapper.addThing(thing);
    }


}
